package cn.edu.henu.team.MPChart;

import butterknife.BindView;
import butterknife.OnClick;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.table.health_f;
import cn.edu.henu.team.table.health_p;
import cn.edu.henu.team.table.healthfUtils;
import cn.edu.henu.team.table.healthpUtils;
import cn.edu.henu.team.utils.CalendarDialog;
import cn.edu.henu.team.utils.ChartUtil;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BarAnalyseActivity extends BaseActivity {
    private int now_year;
    private int now_month;
    private float f1 = 0, f2 = 0, f3 = 0;
    int selectPos = -1, selectMonth = -1, selectYear = -1;
    private List<health_f> data;

    private ImageView back;
    @BindView(R.id.chart_riLi)
    ImageView riLi;

    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.sum)
    TextView testSum;

    private BarChart barChart;
    private Intent mIntent;

    @Override
    protected int initLayout() {
        return R.layout.activity_bar_analyse;
    }

    @Override
    protected void initView() {
        barChart = findViewById(R.id.BarChart);

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Calendar calendar = Calendar.getInstance();
        now_year = calendar.get(Calendar.YEAR);
        now_month = calendar.get(Calendar.MONTH) + 1;
        selectYear = now_year;
        selectMonth = now_month;
        time.setText("当前日期: " + now_year + " 年 " + now_month + "月");

    }

    @Override
    protected void initData() {
        data = healthfUtils.queryListByYearAndMonth(now_year, now_month);
        testSum.setText("当月共评测数据为： "+data.size()+"条");
        for (health_f t : data) {
            f1 += t.getNum1();
            f2 += t.getNum2();
            f3 += t.getNum3();

            //隐藏图例
            barChart.getLegend().setEnabled(false);
            barChart.getDescription().setEnabled(false);
            //取消缩放、点击、高亮效果
            barChart.setScaleEnabled(false);
            barChart.setClickable(false);
            barChart.setHighlightPerDragEnabled(false);
            barChart.setHighlightPerTapEnabled(false);
            //获取X轴
            XAxis xAxis = barChart.getXAxis();
            //设置X轴在下方（默认X轴在上方）
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            //给X轴添加标签
            xAxis.setValueFormatter(new IndexAxisValueFormatter(new String[]{"龋病", "牙髓炎", "根尖周炎"}));
            //取消X轴网格线
            xAxis.setDrawGridLines(false);
            //获取左边Y轴
            YAxis leftYAxis = barChart.getAxisLeft();
            //取消Y轴网格线
            leftYAxis.setDrawGridLines(false);
            //显示左边Y轴的标签
            leftYAxis.setDrawLabels(true);
            //获取右边Y轴
            YAxis rightYAxis = barChart.getAxisRight();
            //隐藏右边Y轴
            rightYAxis.setEnabled(false);

            //添加数据
            List<BarEntry> entries = new ArrayList<>();
            entries.add(new BarEntry(0, f1));
            entries.add(new BarEntry(1, f2));
            entries.add(new BarEntry(2, f3));
            //数据添加到数据集
            BarDataSet dataSet = new BarDataSet(entries, "none");
            //柱体颜色
            dataSet.setColor(Color.parseColor("#FF6363"));
            //显示数值
            dataSet.setDrawValues(true);
            //数据集赋值给数据对象
            BarData data = new BarData(dataSet);
            barChart.setData(data);
            //设置柱体宽带
            data.setBarWidth(0.3f);
        }
    }

    @OnClick({R.id.chart_riLi})
    protected void OnClick(View v) {
        switch (v.getId()) {
            case R.id.chart_riLi:
                showCalendarDialog();
                break;
            default:
                break;
        }
    }

    /* 显示日历对话框*/
    private void showCalendarDialog() {
        CalendarDialog dialog = new CalendarDialog(this, selectPos, selectMonth);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnRefreshListener(new CalendarDialog.OnRefreshListener() {
            @Override
            public void onRefresh(int selPos, int year, int month) {
                BarAnalyseActivity.this.selectPos = selPos;
                BarAnalyseActivity.this.selectYear = year;
                BarAnalyseActivity.this.selectMonth = month;
                time.setText("当前日期: " + year + " 年 " + month + "月");

                data.clear();
                data = healthfUtils.queryListByYearAndMonth(year, selectMonth);
                testSum.setText("当月共评测数据为： "+data.size()+"条");

                f1 = f2 = f3 = 0;
                for (health_f t : data) {
                    f1 += t.getNum1();
                    f2 += t.getNum2();
                    f3 += t.getNum3();

                    List<BarEntry> entries = new ArrayList<>();
                    entries.add(new BarEntry(0, f1));
                    entries.add(new BarEntry(1, f2));
                    entries.add(new BarEntry(2, f3));
                    //数据添加到数据集
                    BarDataSet dataSet = new BarDataSet(entries, "none");
                    //柱体颜色
                    dataSet.setColor(Color.parseColor("#FF6363"));
                    //显示数值
                    dataSet.setDrawValues(true);
                    //数据集赋值给数据对象
                    BarData data = new BarData(dataSet);
                    barChart.setData(data);

                    //设置柱体宽带
                    data.setBarWidth(0.3f);
                }
            }
        });
    }
}